﻿using System.ServiceModel;
using System.ServiceModel.Web;
using Interscape.TwilioClient.Components;
using Microsoft.Http;
using System.Net;

namespace Interscape.TwilioClient.Components
{
    /// <summary>
    /// The base class for functionalty common to the different TwilioClient Managers.
    /// </summary>
    /// <typeparam name="TChannel">An interface that implements the [ServiceContract] attribute to enable WCF communication.</typeparam>
    public abstract class ManagerBase<TChannel> : ClientBase<TChannel> where TChannel : class
    {

        #region Constructors

        /// <summary>
        ///  Creates a new instance of the ManagerBase.
        /// </summary>
        public ManagerBase() : base()
        {
        }

        /// <summary>
        ///  Creates a new instance of the ManagerBase.
        /// </summary>
        /// <param name="var"></param>
        public ManagerBase(TChannel var) : base()
        {
        }

        /// <summary>
        /// Creates a new instance of the ManagerBase.
        /// </summary>
        /// <param name="accountSid">The 34 character string that uniquely identifies the Account which will be used to make the TwilioAPI requests.</param>
        /// <param name="authToken">The "password" for your Twilio API calls.</param>
        public ManagerBase(string accountSid, string authToken) : base()
        {
            this.AccountSid = accountSid;
            this.Urls = new ServiceUrls(accountSid);
            this.ClientCredentials.UserName.UserName = accountSid;
            this.ClientCredentials.UserName.Password = authToken;

            //instantiate the REST Toolkit's HttpClient to help us with POSTs.
            this.HttpClientHelper = new HttpClient();
            this.HttpClientHelper.TransportSettings.Credentials = new NetworkCredential(accountSid, authToken);
        }

        #endregion

        /// <summary>
        /// The 34 character string that uniquely identifies the Account which will be used to make the TwilioAPI requests.
        /// </summary>
        public string AccountSid  { get; private set; }

        /// <summary>
        /// An instance of the ServiceUrls utility class, for getting Twilio API endpoints to be used with WCF REST Toolkit POST and PUT operations.
        /// </summary>
        public ServiceUrls Urls { get; private set; }

        /// <summary>
        /// An instance of the HttpClient helper class from the WCF REST Toolkit, which is used for POST and PUT operations.
        /// </summary>
        public HttpClient HttpClientHelper { get; private set; }

    }
}
